﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Xrm.Solutions.FrameworkSample.Plugins;
using Microsoft.QualityTools.Testing.Fakes.Stubs;
using Microsoft.QualityTools.Testing.Fakes;
using Xrm.Framework.Test.Unit.Fakes;


namespace Xrm.Solutions.FrameworkSample.XrmUnitTests.Plugins
{
    [TestClass]
    public class TestRunIdentityCheckXrmUsingShim : PluginUnitTest
    {
        #region Instance Variables

        //TODO: Declare your variables for setup and verification
        private Guid contactId = Guid.NewGuid();
        private int score = 99;

        #endregion

        #region Setup

        protected override IPlugin SetupPlugin()
        {
            //TODO: Setup your trigger
            base.SetPluginEvent("xdft_identitycheck", "Create",
               Xrm.Framework.Test.Unit.SdkMessageProcessingStepImage.PostOperation);

            //TODO: Setup your Stubs & Shim

            Entity identityCheck = new Entity("contact");
            identityCheck["xdft_firstname"] = "Wael";
            identityCheck["xdft_lastname"] = "Hamze";
            identityCheck["xdft_dateofbirth"] = DateTime.Now;
            identityCheck["xdft_address"] = "London";
            identityCheck["xdft_contactid"] = new EntityReference("contact", new Guid());

            base.SetTarget(identityCheck);

            Xrm.Solutions.FrameworkSample.Common.Fakes.ShimIdentityCheckService.AllInstances.RunCheckStringStringDateTimeString =
                (service, firstname, lastname, dob, address) =>
                {
                    if (firstname == "Wael")
                        return 99;
                    else
                        return 60;
                };

            base.OrganizationServiceStub.InstanceObserver = new StubObserver();

            //TODO: Create your Plugin
            IPlugin plugin = new RunIdentityCheck();

            return plugin;
        }

        #endregion

        #region Test

        [TestMethod]
        public void RunTestRunIdentityCheckXrmUsingShim()
        {
            using (ShimsContext.Create())
            {
                base.Test();
            }
        }

        #endregion

        #region Verify

        protected override void Verify()
        {
            Assert.IsNull(Error);

            bool called = ((StubObserver)base.OrganizationServiceStub.InstanceObserver).GetCalls().Any(call =>
            {
                if (call.StubbedMethod.Name == "Update")
                {
                    Entity contact = (Entity)call.GetArguments()[0];

                    if ((int)contact["xdft_identityscore"] == score)
                    {
                        return true;
                    }
                }
                return false;
            }
            );

            Assert.IsTrue(called);
        }

        #endregion
    }
}
